import { CreepRole, RoleDatas } from "@/role/types/role"
import CreepController from "../controller"


class RoleManager {

    /**
     *
     * @param creep
     * @returns
     */
    static loop(creep: Creep) {

        if (creep.ticksToLive < 50 && creep.store[RESOURCE_ENERGY] > 0) {
            const storage = CreepController.findStorage(creep)
            if (storage) {
                const status = creep.transfer(storage, RESOURCE_ENERGY)
                if (status == ERR_NOT_IN_RANGE) {
                    creep.goTo(storage.pos)
                }
                else if (status == OK) {
                    if (creep.store[RESOURCE_ENERGY] == 0) {
                        creep.suicide()
                    }
                }
                return
            }
        }

        const memoryData: RoleDatas[CreepRole.RemoteManager] = creep.memory.data as RoleDatas[CreepRole.RemoteManager]

        if (!memoryData) {
            creep.log('memory error')
            return
        }

        if (creep.memory.working && creep.store[RESOURCE_ENERGY] == 0) {
            creep.memory.working = false;
            creep.say('🔄 take');
        }
        if (!creep.memory.working && creep.store[RESOURCE_ENERGY] >= 100) {
            creep.memory.working = true;
            creep.say('🚧 carry');
        }

        if (!creep.memory.working && creep.ticksToLive > 65) {

            if (!memoryData.resourceId && !memoryData.sourceId) {
                const droppedEnergy = CreepController.managerFindDropSource(creep, 200)
                if (droppedEnergy) {
                    memoryData.resourceId = droppedEnergy.id
                }
                else {
                    const container = CreepController.findContainer(creep, 300)
                    if (container) {
                        memoryData.sourceId = container.id
                    }
                }
            }

            if (memoryData.resourceId) {
                const target = Game.getObjectById(memoryData.resourceId) as Resource
                if (target) {
                    const resource = target as unknown as Resource
                    if (resource) {
                        creep.say('=>res')
                        creep.goTo(target.pos, { range: 1 })
                        const status = creep.pickup(resource)
                        if (status == OK) {
                            delete memoryData.resourceId
                        }
                        return
                    }
                }
                delete memoryData.resourceId
            }

            if (memoryData.sourceId) {
                const container = Game.getObjectById(memoryData.sourceId) as StructureContainer
                if (container) {
                    creep.say('=>con')
                    creep.goTo(container.pos, { range: 1 })
                    const status = creep.withdraw(container, RESOURCE_ENERGY)
                    if (status == OK) {
                        delete memoryData.sourceId
                    }
                    return
                }
                delete memoryData.sourceId
            }
        }
        else {

            if (!memoryData.sourceId) {
                const storage = CreepController.managerFindStorage(creep)
                if (storage) {
                    memoryData.sourceId = storage.id
                }
            }

            if (memoryData.sourceId) {
                const target = Game.getObjectById(memoryData.sourceId) as StructureWithStore
                if (target && target.store.getFreeCapacity(RESOURCE_ENERGY) > 0) {
                    creep.goTo(target.pos)
                    const status = creep.transfer(target, RESOURCE_ENERGY)
                    if (status == OK) {
                        delete memoryData.sourceId
                        if (creep.store[RESOURCE_ENERGY] < 50) {
                            creep.memory.working = false
                        }
                    }
                }
                delete memoryData.sourceId
            }
        }
    }
}

export default RoleManager
